package com.gmail.huxiaowi.cblog.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gmail.huxiaowi.cblog.entity.Article;
import com.gmail.huxiaowi.cblog.util.Utils;
import com.gmail.huxiaowi.cblog.web.freemarker.FreemarkerSupport;

import freemarker.template.Template;

@Controller
@RequestMapping("/admin/control.do")
public class AdminController extends FreemarkerSupport {
    
    @RequestMapping(method = {RequestMethod.GET}, params = "action=createArticlePage")
    public void createArticlePage(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Template tl = getTemplate(getServletContext(request), "/ftl", "createArticle.ftl");
        
        Map<String, Object> dataModel = new HashMap<String, Object>();
        dataModel.put("categorys", userBiz.queryAllCategorys());
        tl.process(dataModel, response.getWriter());
    }
    
    @RequestMapping(method = {RequestMethod.POST}, params = "action=createArticle")
    public void createArticle(Long categoryId, String title, String content, String postName) {
        userBiz.saveArticle(categoryId, new Article(postName, title, content, Utils.getGMT8CurTime()));
    }
    
    @RequestMapping(method = {RequestMethod.GET}, params = "action=createCategoryPage")
    public void createCategoryPage(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Template tl = getTemplate(getServletContext(request), "/ftl", "createCategory.ftl");
        
        Map<String, Object> dataModel = new HashMap<String, Object>();
        dataModel.put("categorys", userBiz.queryAllCategorys());
        tl.process(dataModel, response.getWriter());
    }
}
